# -*- makefile -*-
#
# Makefile for building NASM using Borland C/C++ and Borland MAKE.
# http://www.codegear.com
#

srcdir = .

CC = bcc32.exe
LINK = ilink32.exe
LIB = tlib.exe
PERL = perl -I$(srcdir)\perllib

# for Borland C/C++ Compilers
CFLAGS_BASE = -c -6 -a8 -tW -tWM -tWR -g255
				#-K-
				#-DWIN32 -DUNICODE -D_UNICODE
LFLAGS_BASE = -c -C

!if $(BUILD) == release
    CFLAGS_BASE = $(CFLAGS_BASE) -O2
    LFLAGS_BASE = $(LFLAGS_BASE) -x
!else
    CFLAGS_BASE = $(CFLAGS_BASE) -v -y
    LFLAGS_BASE = $(LFLAGS_BASE) -v -m -M -s
!endif

BUILD_CFLAGS	= $(CFLAGS_BASE) -I$(srcdir)\inttypes
INTERNAL_CFLAGS = -I$(srcdir) -I. -DHAVE_SNPRINTF -DHAVE_VSNPRINTF
CFLAGS = $(BUILD_CFLAGS) $(INTERNAL_CFLAGS)
LFLAGS = -ap -Tpe $(LFLAGS_BASE)

debug:
	$(MAKE) BUILD=debug    -f Mkfiles\bcc32.mak  all
release:
	$(MAKE) BUILD=release  -f Mkfiles\bcc32.mak  all

NASM =	nasm.obj nasmlib.obj ver.obj \
	raa.obj saa.obj rbtree.obj \
	float.obj insnsa.obj insnsb.obj \
	directiv.obj \
	assemble.obj labels.obj hashtbl.obj crc64.obj parser.obj \
	output\outform.obj output\outlib.obj output\nulldbg.obj \
	output\nullout.obj \
	output\outbin.obj output\outaout.obj output\outcoff.obj \
	output\outelf.obj output\outelf32.obj output\outelf64.obj \
	output\outelfx32.obj \
	output\outobj.obj output\outas86.obj output\outrdf2.obj \
	output\outdbg.obj output\outieee.obj output\outmac32.obj \
	output\outmac64.obj preproc.obj quote.obj pptok.obj \
	macros.obj listing.obj eval.obj exprlib.obj stdscan.obj \
	strfunc.obj tokhash.obj regvals.obj regflags.obj \
	ilog2.obj \
	lib\strlcpy.obj \
	preproc-nop.obj

NDISASM = ndisasm.obj disasm.obj sync.obj nasmlib.obj ver.obj \
	insnsd.obj insnsb.obj insnsn.obj regs.obj regdis.obj

all: nasm.exe ndisasm.exe

nasm.exe: $(NASM)
	$(LINK) $(LFLAGS) c0x32 $(NASM), nasm.exe, nasm.map, import32.lib cw32mti.lib

ndisasm.exe: $(NDISASM)
	$(LINK) $(LFLAGS) c0x32 $(NDISASM), ndisasm.exe, ndisasm.map, import32.lib cw32mti.lib

.c.obj:
	$(CC) $(CFLAGS) -o$@ $<
#{output\}.c{output\}.obj:
#    $(CC) $(CFLAGS) -ooutput\$&.obj $<
#{lib\}.c{lib\}obj:
#    $(CC) $(CFLAGS) -olib\$&.obj $<
	
# These source files are automagically generated from a single
# instruction-table file by a Perl script. They're distributed,
# though, so it isn't necessary to have Perl just to recompile NASM
# from the distribution.

insnsb.c: insns.dat insns.pl
	$(PERL) $(srcdir)\insns.pl -b $(srcdir)\insns.dat
insnsa.c: insns.dat insns.pl
	$(PERL) $(srcdir)\insns.pl -a $(srcdir)\insns.dat
insnsd.c: insns.dat insns.pl
	$(PERL) $(srcdir)\insns.pl -d $(srcdir)\insns.dat
insnsi.h: insns.dat insns.pl
	$(PERL) $(srcdir)\insns.pl -i $(srcdir)\insns.dat
insnsn.c: insns.dat insns.pl
	$(PERL) $(srcdir)\insns.pl -n $(srcdir)\insns.dat

# These files contains all the standard macros that are derived from
# the version number.
version.h: version version.pl
	$(PERL) $(srcdir)\version.pl h < $(srcdir)\version > version.h

version.mac: version version.pl
	$(PERL) $(srcdir)\version.pl mac < $(srcdir)\version > version.mac

# This source file is generated from the standard macros file
# `standard.mac' by another Perl script. Again, it's part of the
# standard distribution.

macros.c: macros.pl standard.mac version.mac
	$(PERL) $(srcdir)\macros.pl $(srcdir)\standard.mac version.mac

# These source files are generated from regs.dat by yet another
# perl script.
regs.c: regs.dat regs.pl
	$(PERL) $(srcdir)\regs.pl c $(srcdir)\regs.dat > regs.c
regflags.c: regs.dat regs.pl
	$(PERL) $(srcdir)\regs.pl fc $(srcdir)\regs.dat > regflags.c
regdis.c: regs.dat regs.pl
	$(PERL) $(srcdir)\regs.pl dc $(srcdir)\regs.dat > regdis.c
regvals.c: regs.dat regs.pl
	$(PERL) $(srcdir)\regs.pl vc $(srcdir)\regs.dat > regvals.c
regs.h: regs.dat regs.pl
	$(PERL) $(srcdir)\regs.pl h $(srcdir)\regs.dat > regs.h

# Assembler token hash
tokhash.c: insns.dat regs.dat tokens.dat tokhash.pl perllib\phash.ph
	$(PERL) $(srcdir)\tokhash.pl c $(srcdir)\insns.dat $(srcdir)\regs.dat \
		$(srcdir)\tokens.dat > tokhash.c

# Assembler token metadata
tokens.h: insns.dat regs.dat tokens.dat tokhash.pl perllib\phash.ph
	$(PERL) $(srcdir)\tokhash.pl h $(srcdir)\insns.dat $(srcdir)\regs.dat \
		$(srcdir)\tokens.dat > tokens.h

# Preprocessor token hash
pptok.h: pptok.dat pptok.pl perllib\phash.ph
	$(PERL) $(srcdir)\pptok.pl h $(srcdir)\pptok.dat pptok.h
pptok.c: pptok.dat pptok.pl perllib\phash.ph
	$(PERL) $(srcdir)\pptok.pl c $(srcdir)\pptok.dat pptok.c

# This target generates all files that require perl.
# This allows easier generation of distribution (see dist target).
PERLREQ = macros.c insnsb.c insnsa.c insnsd.c insnsi.h insnsn.c \
	  regs.c regs.h regflags.c regdis.c regvals.c tokhash.c tokens.h \
	  version.h version.mac pptok.h pptok.c
perlreq: $(PERLREQ)

clean:
    @echo Removing ...
    rm -f *.obj
	rm -f */*.obj
	rm -f *.obj
	rm -f *.exe
	rm -f *.map
	rm -f *.tds
	rm -f *.il*
    @echo clean finished.


	
	
	
	
